﻿namespace SPDeploy
{
    using System;
    using System.Collections.Generic;
    using System.Reflection;
    using Library.Processing.Library;
    using NDesk.Options;

    public class Program
    {
        #region Methods: private

        private static int Main(string[] args)
        {
            ShowLogo();

            // NDesk.Options is somewhat of a horror syntax-wise, but its light weight, so I'm using that for now
            string configFile = null;
            string modeString = null;
            bool showHelp = false;
            int verbosity = 0;
            var p = new OptionSet
                        {
                            {"f|file=", "Deployment configuration file", v => configFile = v},
                            {"m|mode=", "Either 'install' or 'uninstall'", v => modeString = v},
                            {"v|verbose", "Supply -v, -vv or -vvv for more log spam", v => { ++verbosity; }},
                            {"h|help", "show this message and exit", v => showHelp = v != null}
                        };

            ProcessorModes mode = ProcessorModes.Install;
            List<string> extra;
            try
            {
                extra = p.Parse(args);

                if (configFile == null)
                {
                    throw new OptionException("The file option must be specified", "file");
                }
                if (modeString == null)
                {
                    throw new OptionException("The mode option must be specified", "modeString");
                }

                if (modeString.Equals("install", StringComparison.InvariantCultureIgnoreCase))
                {
                    mode = ProcessorModes.Install;
                }
                else if (modeString.Equals("uninstall", StringComparison.InvariantCultureIgnoreCase))
                {
                    mode = ProcessorModes.Uninstall;
                }
                else
                {
                    throw new OptionException("The mode option must either be install or uninstall", "modeString");
                }
            }
            catch (OptionException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine();
                showHelp = true;
            }

            if (showHelp)
            {
                ShowHelp(p);
                return -1;
            }

            // Amazingly, NDesk.Options has done its job - so get cracking on the real stuff
            var core = new SPDeployCore();
            return core.ProcessDeploymentConfiguration(configFile, mode, verbosity) ? 0 : -1;
        }

        private static void ShowHelp(OptionSet p)
        {
            Console.WriteLine("Usage: spdeploy [OPTIONS]");
            Console.WriteLine();
            Console.WriteLine("Options:");
            p.WriteOptionDescriptions(Console.Out);
        }

        private static void ShowLogo()
        {
            Version version = Assembly.GetExecutingAssembly().GetName().Version;
            Console.Out.WriteLine("SPDeploy v{0}.{1}.{2} - http://spdeploy.codeplex.com", version.Major, version.Minor, version.Build);
            Console.Out.WriteLine("By Einar Otto Stangvik (www.CodeForNuts.com)");
            Console.Out.WriteLine();
        }

        #endregion
    }
}